﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.teachers
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        student stud = new student();
        Classes cl = new Classes();
        teacher teach = new teacher();
        user_types ut = new user_types();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                btnupdate.Visible = false;
                //Panel2.Visible = false;
                for (int i = 1; i < 31; i++)
                    dddate.Items.Add(i.ToString());
                for (int j = 1950; j < 2050; j++)
                {
                    ddyear.Items.Add(j.ToString());
                    ddyearstuding.Items.Add(j.ToString());
                }
                Panel1.Visible = false; Panel2.Visible = false;
                DataTable dt1 = teach.GetRecords(Convert.ToInt64(Session["userid"]), "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, " and ");
                DataTable dt2 = cl.GetRecords(-99, "",-99, false, " and ");
                ddclass.DataSource = dt2;
                ddclass.DataTextField = "class_name";
                ddclass.DataValueField = "class_id";
                ddclass.DataBind();
               //ddclass0.DataSource = dt2;
                //ddclass0.DataTextField = "class_name";
                //ddclass0.DataValueField = "class_id";
                //ddclass0.DataBind();
                DataTable dt3=ut.GetRecords(-99,"",false," and ");
                ddusertype.DataSource = dt3;
                ddusertype.DataTextField = "user_type";
                ddusertype.DataValueField = "user_type_id";
                ddusertype.DataBind();
                DataTable dt = stud.GetRecords(-99, "", -99, Convert.ToInt64(dt2.Rows[0]["class_id"].ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
                if (Convert.ToInt64(Session["userid"].ToString()) == Convert.ToInt64(dt2.Rows[0]["teacher_id"].ToString()))
                {
                    DataTable dtc = cl.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["teacher_id"].ToString()), false, " and ");
                    tbclass.Text = dtc.Rows[0]["class_name"].ToString();
                
                    Panel1.Visible = false; Panel2.Visible = true; Panel3.Visible = true;
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
                else
                {
                    Panel1.Visible = true;
                    Panel2.Visible = false; Panel3.Visible = false;
                    GridView2.DataSource = dt;
                    GridView2.DataBind();
                }        
            }
        }



        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            Image2.Visible = true;
            //Panel2.Visible = true;
            btnupdate.Visible = true;
            btninsert.Visible = false;
            labusername.Visible = false; tbusername.Visible = false;
            labpassword.Visible = false; tbpassword.Visible = false;
            DataTable dt = stud.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
            tbstudentid.Text = dt.Rows[0]["student_id"].ToString();
            tbenroll.Text = dt.Rows[0]["enrollment_no"].ToString();
            tbroll.Text = dt.Rows[0]["roll_no"].ToString();
            DateTime dat1 = new DateTime();
            dat1 = Convert.ToDateTime(dt.Rows[0]["year_of_studing"].ToString());
            ddyearstuding.SelectedValue = dat1.Year.ToString();
            tbstudname.Text = dt.Rows[0]["student_name"].ToString();
            DateTime dat = new DateTime();
            dat = Convert.ToDateTime(dt.Rows[0]["date_of_birth"].ToString());
            dddate.SelectedValue = dat.Day.ToString();
            ddmonth.SelectedValue = dat.Month.ToString();
            ddyear.SelectedValue = dat.Year.ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["gender"].ToString();
            TextBox1.Text = dt.Rows[0]["photo"].ToString();
            Image2.ImageUrl = "~/admin/student/" + TextBox1.Text;
            tbpaddess.Text = dt.Rows[0]["permanent_address"].ToString();
            tbrespno.Text = dt.Rows[0]["residence_pno"].ToString();
            tbmobile.Text = dt.Rows[0]["mobile_no"].ToString();
            tbstudmail.Text = dt.Rows[0]["student_emailid"].ToString();
            ddusertype.SelectedValue = dt.Rows[0]["user_type_id"].ToString();
            ddclass.SelectedValue = dt.Rows[0]["class_id"].ToString();
            tbusername.Text = dt.Rows[0]["username"].ToString();
            tbpassword.Text = dt.Rows[0]["password"].ToString();

        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = stud.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
                stud.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                stud.enrollment_no = dt.Rows[0]["enrollment_no"].ToString();
                stud.roll_no = Convert.ToInt64(dt.Rows[0]["roll_no"].ToString());
                stud.year_of_studing = dt.Rows[0]["year_of_studing"].ToString();
                stud.student_name = dt.Rows[0]["student_name"].ToString();
                stud.date_of_birth = dt.Rows[0]["date_of_birth"].ToString();
                stud.gender = dt.Rows[0]["gender"].ToString();
                stud.photo = dt.Rows[0]["photo"].ToString();
                stud.permanent_address = dt.Rows[0]["permanent_address"].ToString();
                stud.residence_pno = Convert.ToInt64(dt.Rows[0]["residence_pno"].ToString());
                stud.mobile_no = Convert.ToInt64(dt.Rows[0]["mobile_no"].ToString());
                stud.student_emailid = dt.Rows[0]["student_emailid"].ToString();
                stud.user_type_id = Convert.ToInt64(dt.Rows[0]["user_type_id"].ToString());
                stud.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                stud.username = dt.Rows[0]["username"].ToString();
                stud.password = dt.Rows[0]["password"].ToString();
                stud.isdeleted = true;
                stud.SaveRecords();
                Response.Redirect("~/teachers/TSProfile.aspx");
                stud.SaveRecords();
            }
        }

        protected void lbpinfo_Click(object sender, EventArgs e)
        {
            Response.Redirect("TPProfile.aspx");
        }

        protected void ddclass_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = stud.GetRecords(-99, "", -99, Convert.ToInt64(ddclass.SelectedValue.ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }

        protected void dddate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddmonth_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddyear_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddyearstuding_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddclass0_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            stud.student_id = Convert.ToInt64(tbstudentid.Text);
            stud.enrollment_no = tbenroll.Text;
            stud.roll_no = Convert.ToInt64(tbroll.Text);
            stud.year_of_studing = ddyearstuding.SelectedValue.ToString();
            stud.student_name = tbstudname.Text;
            DateTime dat = new DateTime(Convert.ToInt32(ddyear.SelectedItem.ToString()), Convert.ToInt32(ddmonth.SelectedValue.ToString()), Convert.ToInt32(dddate.SelectedItem.ToString()));
            stud.date_of_birth = dat.ToString();
            stud.gender = RadioButtonList1.SelectedValue.ToString();
            if (FileUpload1.HasFile)
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/student/" + FileUpload1.FileName));
                stud.photo = FileUpload1.FileName;
            }
            else
                stud.photo = TextBox1.Text;
            stud.permanent_address = tbpaddess.Text;
            stud.residence_pno = Convert.ToInt64(tbrespno.Text);
            stud.mobile_no = Convert.ToInt64(tbmobile.Text);
            stud.student_emailid = tbstudmail.Text;
            stud.user_type_id = Convert.ToInt64(ddusertype.SelectedValue.ToString());
            DataTable dt1 = teach.GetRecords(Convert.ToInt64(Session["userid"]), "", "", "", "", "", "", -99, -99, -99, "", "", "", "", "", false, " and ");
            DataTable dtc = cl.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["teacher_id"].ToString()), false, " and ");
            stud.class_id = Convert.ToInt64(dtc.Rows[0]["class_id"].ToString());
            stud.username = tbusername.Text;
            stud.password = tbpassword.Text;
            stud.isdeleted = false;
            stud.SaveRecords();
            Response.Redirect("~/teachers/TSProfile.aspx");
        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            DataTable dt = stud.GetRecords(-99, "", -99, Convert.ToInt64(ddclass.SelectedValue.ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
            GridView1.DataSource = dt;
            GridView1.DataBind();
            
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            DataTable dt = cl.GetRecords(-99,"",Convert.ToInt64(Session["userid"]),false," and ");
            DataTable dt1 = stud.GetRecords(-99, "", -99, Convert.ToInt64(dt.Rows[0]["class_id"].ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
            GridView2.DataSource = dt1;
            GridView2.DataBind();
        }
        protected void showerrorenroll()
        {
            string msg = "Enrollment no already exist";
            string error = "<script type=\"text/javascript\">alert('" + msg + "');</script>";
            // Gets the executing web page 
            Page page = HttpContext.Current.CurrentHandler as Page;

            // Checks if the handler is a Page and that the script isn't allready on the Page 
            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterClientScriptBlock(typeof(WebForm10), "alert", error);
            }
            

        }
        protected void showerrorroll()
        {
            string msg = "Roll no already exist in this class";
            string error = "<script type=\"text/javascript\">alert('" + msg + "');</script>";
            // Gets the executing web page 
            Page page = HttpContext.Current.CurrentHandler as Page;

            // Checks if the handler is a Page and that the script isn't allready on the Page 
            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
            {
                page.ClientScript.RegisterClientScriptBlock(typeof(WebForm10), "alert", error);
            }
        }
        protected void btninsert_Click(object sender, EventArgs e)
        {
            
            DataTable dt = stud.GetRecords(-99,tbenroll.Text, -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
            if (dt.Rows.Count > 0)
            {
                showerrorenroll();         
            }
            DataTable dtcc = cl.GetRecords(-99,"",Convert.ToInt64(Session["userid"]),false," and ");
            DataTable dtc1 = stud.GetRecords(-99, "",Convert.ToInt64(tbroll.Text), Convert.ToInt64(dtcc.Rows[0]["class_id"].ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
            if (dtc1.Rows.Count > 0)
            {
                showerrorroll();
            }
            else
            {
                stud.student_id = -99;
                stud.enrollment_no = tbenroll.Text;
                stud.roll_no = Convert.ToInt64(tbroll.Text);
                stud.year_of_studing = ddyearstuding.SelectedValue.ToString();
                stud.student_name = tbstudname.Text;
                DateTime dat = new DateTime(Convert.ToInt32(ddyear.SelectedItem.ToString()), Convert.ToInt32(ddmonth.SelectedValue.ToString()), Convert.ToInt32(dddate.SelectedItem.ToString()));
                stud.date_of_birth = dat.ToString();
                stud.gender = RadioButtonList1.SelectedValue.ToString();
                if (FileUpload1.HasFile)

                    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/student/" + FileUpload1.FileName));
                stud.photo = FileUpload1.FileName;

                //else
                //    stud.photo = TextBox1.Text;
                stud.permanent_address = tbpaddess.Text;
                stud.residence_pno = Convert.ToInt64(tbrespno.Text);
                stud.mobile_no = Convert.ToInt64(tbmobile.Text);
                stud.student_emailid = tbstudmail.Text;
                stud.user_type_id = Convert.ToInt64(ddusertype.SelectedValue.ToString());
                DataTable dt1 = teach.GetRecords(Convert.ToInt64(Session["userid"]), "", "", "", "", "", "", -99, -99, -99, "", "", "", "", "", false, " and ");
                DataTable dtc = cl.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["teacher_id"].ToString()), false, " and ");

                stud.class_id = Convert.ToInt64(dtc.Rows[0]["class_id"].ToString());
                //stud.username = tbusername.Text;
                stud.username = "S" + tbenroll.Text.Substring(0,3)+tbstudname.Text.Substring(0,3);
                //stud.password = tbpassword.Text;
                stud.password = "S" + tbstudname.Text.Substring(0, 3)+ tbenroll.Text.Substring(0, 3);
                stud.isdeleted = false;
                stud.SaveRecords();
                Response.Redirect("~/teachers/TSProfile.aspx");
            }
                     
        }

        
    }
}
